package org.javaboy.client01.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.Map;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public Object hello() {
        return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    //    @Autowired
    OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager;

    @GetMapping("/token")
    public String getToken(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
        //1. 构造一个获取令牌的请求
        OAuth2AuthorizeRequest req = OAuth2AuthorizeRequest.withClientRegistrationId("client01")
                .principal(authentication)
                .attributes(attrs -> {
                    attrs.put(HttpServletRequest.class.getName(), request);
                    attrs.put(HttpServletResponse.class.getName(), response);
                })
                .build();
        //2. 获取令牌
        OAuth2AuthorizedClient client = oAuth2AuthorizedClientManager.authorize(req);
        OAuth2AccessToken accessToken = client.getAccessToken();
        OAuth2RefreshToken refreshToken = client.getRefreshToken();
        System.out.println("accessToken = " + accessToken);
        System.out.println("refreshToken = " + refreshToken);
        return accessToken.getTokenValue() + ">>>";
    }

    @GetMapping("/token2")
    public String getToken2(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient client) {
        OAuth2AccessToken accessToken = client.getAccessToken();
        OAuth2RefreshToken refreshToken = client.getRefreshToken();
        System.out.println("accessToken = " + accessToken);
        System.out.println("refreshToken = " + refreshToken);
        return accessToken.getTokenValue() + ">>>" + refreshToken.getTokenValue();
    }
    @GetMapping("/token3")
    public OAuth2AccessToken getToken3(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient client) {
        OAuth2AccessToken accessToken = client.getAccessToken();
        return accessToken;
    }

    @GetMapping("/hello3")
    public String helloJavaboy(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient client) {
        OAuth2AccessToken accessToken = client.getAccessToken();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Bearer " + accessToken.getTokenValue());
        HttpEntity<?> httpEntity = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.exchange("http://res.javaboy.org:8081/hello2", HttpMethod.GET, httpEntity, String.class);
        String body = response.getBody();
        System.out.println("response.getBody() = " + body);
        return body;
    }

    @Autowired
    WebClient webClient;

    @GetMapping("/hello4")
    public String hello4(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient client) {
        String block = webClient.get().uri("http://res.javaboy.org:8081/hello2")
                .attributes(ServerOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient(client))
                .retrieve()
                .bodyToMono(String.class)
                .block();
        return block;
    }
    @GetMapping("/hello5")
    public Map<String, Object> hello5(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient client) {
        RestTemplate restTemplate = new RestTemplate();
        LinkedMultiValueMap params = new LinkedMultiValueMap();
        params.add("client_id", client.getClientRegistration().getClientId());
        params.add("client_secret", client.getClientRegistration().getClientSecret());
        params.add("grant_type", "refresh_token");
        params.add("refresh_token", client.getRefreshToken().getTokenValue());
        Map<String, Object> accessTokenMap = restTemplate.postForObject("http://auth.javaboy.org:8080/oauth2/token", params, Map.class);
        System.out.println("accessTokenMap = " + accessTokenMap);
        return accessTokenMap;
    }

}
